package com.tm.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tm.CoreConstants;
import com.tm.model.EcTypeInfo;
import com.tm.model.SpLimitTimeShop;
import com.tm.model.SpSkuInfo;
import com.tm.model.SpSpellGroup;
import com.tm.service.CommonService;
import com.tm.service.SpSkuInfoService;
import com.tm.util.DateUtilsEx;

@Controller
@RequestMapping(value = "/skuView")
public class SpSkuViewController {
	
	@Autowired
	private SpSkuInfoService ssiService;
	
	@Autowired
	private CommonService cService;

	/**
	 * 跳转商品分类页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/skuType")
	public String skuType(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		
		if(request.getSession().getAttribute("memberId") == null){
			response.sendRedirect(CoreConstants.SJ_DOMAIN);
		}
		
		List<EcTypeInfo> list = cService.getEcTypeInfoList();
		
		model.addAttribute("list", list);
		
		return "sku/sku-type";
	}
	
	/**
	 * 跳转指定分类商品列表页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/listByType")
	public String listByType(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		
		if(request.getSession().getAttribute("memberId") == null){
			response.sendRedirect(CoreConstants.SJ_DOMAIN);
		}
		
		Integer typeId = Integer.parseInt(request.getParameter("typeId"));
		
		EcTypeInfo type = cService.getEcTypeInfoByTypeId(typeId);
		
		model.addAttribute("typeId", typeId);
		model.addAttribute("type", type);
		
		return "sku/list";
	}
	
	/**
	 * 跳转商品详情页
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 */
	@RequestMapping(value = "/detail")
	public String detail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ParseException, IOException {
		
		if(StringUtils.isNotEmpty(request.getParameter("storeId"))){
			request.getSession().setAttribute("storeId", request.getParameter("storeId"));
		}
		
		if(request.getSession().getAttribute("memberId") == null){
			response.sendRedirect(CoreConstants.SJ_DOMAIN);
		}
		
		Integer skuId = Integer.parseInt(request.getParameter("skuId"));
		Integer memberId = (Integer) request.getSession().getAttribute("memberId");
		
		//获取商品基本信息
		SpSkuInfo sku = ssiService.getSpSkuInfoBySkuId(skuId);
		model.addAttribute("sku", sku);
		
		model.addAttribute("score", sku.getTwMoney().intValue());
		
		//获取商品标签属性
		List<Map<String, Object>> tagList = ssiService.getSpSkuTagListBySkuId(skuId);
		model.addAttribute("tagList", tagList);
		
		//获取商品团购信息
		SpSpellGroup group = ssiService.getSpSpellGroupBySkuId(skuId);
		model.addAttribute("group", group);
		if(group != null){
			long secs = cService.getSecs(group.getEndDate());
			model.addAttribute("secs", secs);
//			Date a = DateUtilsEx.formatToDate(group.getEndDate(), DateUtilsEx.DATE_FORMAT_SEC);
//			Date b = new Date();
//			model.addAttribute("secs", a.getTime() - b.getTime());
		}
		
		//获取商品直降信息
		SpLimitTimeShop limit = ssiService.getSpLimitTimeShopBySkuId(skuId);
		model.addAttribute("limit", limit);
		if(limit != null){
			long secs = cService.getSecs(limit.getEndDate());
			model.addAttribute("secs", secs);
//			Date a = DateUtilsEx.formatToDate(limit.getEndDate(), DateUtilsEx.DATE_FORMAT_SEC);
//			Date b = new Date();
//			model.addAttribute("secs", a.getTime() - b.getTime());
		}
		
		//获取商品banner展示图
		List<Map<String, Object>> bannerList = ssiService.getSkuPicBySkuId(skuId);
		model.addAttribute("bannerList", bannerList);
		
		//判断商品是否已收藏
		int i = ssiService.countSpSkuCollect(skuId, memberId);
		boolean isCollect = false;
		if(i > 0){
			isCollect = true;
		}
		
		model.addAttribute("isCollect", isCollect);
		
		//获取会员当前购物车商品数量
		int gwcCnt = ssiService.getGwcCntByMemberId(memberId);
		model.addAttribute("gwcCnt", gwcCnt);
		
		if(group != null)
			return "sku/group-detail";
		else if(limit != null)
			return "sku/limit-detail";
		else
			return "sku/detail";
	}
	
	/**
	 * 跳转至关键字搜索商品列表页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/listByKeyword")
	public String listByKeyword(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		
		if(request.getSession().getAttribute("memberId") == null){
			response.sendRedirect(CoreConstants.SJ_DOMAIN);
		}
		
		String keyword =  java.net.URLDecoder.decode(request.getParameter("keyword"));//搜索关键词
		
		model.addAttribute("keyword", keyword);
		
		return "sku/keyword-list";
	}
	
}
